package com.gss.entity;

import com.gss.entity.enums.ConstantEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author DYang
 * @date 2023/5/9
 * 返回信息
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {

    private Integer code;
    private boolean success;
    private String desc;
    private String msg;
    private T respData;


    public static <T> R<T> ok(T res){
        R<T> r = new R<>();
        r.setCode(200);
        r.setSuccess(true);
        r.setRespData(res);
        return r;
    }

    public static R<String> ok(ConstantEnum successCode){
        R<String> r = new R<>();
        r.setSuccess(false);
        r.setRespData(successCode.getMsg());
        r.setCode(successCode.getCode());
        return r;
    }

    public static <T> R<T> error(Integer code,String msg){
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setMsg(msg);
        r.setCode(code);
        return r;
    }

    public static <T> R<T> error(ConstantEnum errorCode){
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setMsg(errorCode.getMsg());
        r.setCode(errorCode.getCode());
        return r;
    }
}
